package com.ruoyi.project.pay.service.handler;


import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.PayOutMessage;
import com.egzosn.pay.common.exception.PayErrorException;
import com.egzosn.pay.wx.bean.WxPayMessage;
import com.ruoyi.project.wx.dao.OrderOrderDao;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * 微信支付回调处理器
 * Created by ZaoSheng on 2016/6/1.
 */


public class WxPayMessageHandler extends BasePayMessageHandler<WxPayMessage, PayService> {


    @Autowired
    private OrderOrderDao orderOrderDao;

    public WxPayMessageHandler(Integer payId) {
        super(payId);
    }

    @Override
    public PayOutMessage handle(WxPayMessage payMessage, Map<String, Object> context, PayService payService) throws PayErrorException {
        //交易状态
        if ("SUCCESS".equals(payMessage.getPayMessage().get("result_code"))){
           /* /////这里进行成功的处理
            System.out.println("payMessage:"+payMessage.toString());
            System.out.println("context:"+context);
            System.out.println("payService:"+payService);
            BigDecimal payAmount = payMessage.getTotalFee();
            String out_trade_no = (String) payMessage.getOutTradeNo();
            System.out.println("out_trade_no:"+out_trade_no);
            List<OrderOrderDO> orderList =  orderOrderDao.getByPayNum(out_trade_no);
            System.out.println(orderList.size());
            if(orderList.size()>0){
                for (OrderOrderDO orderOrderDO : orderList) {
                    orderOrderDO.setStatus(3);
                    orderOrderDO.setPayAt(new Date());
                    orderOrderDO.setPaytype(1);
                    orderOrderDao.update(orderOrderDO);
                }
            }else {
                OrderOrderDO byOrder = orderOrderDao.getByOrder(out_trade_no);
                if(byOrder!=null){
                    byOrder.setStatus(3);
                    byOrder.setPayAt(new Date());
                    byOrder.setPaytype(1);
                    orderOrderDao.update(byOrder);
                }else{
                    return payService.getPayOutMessage("fail", "失败");
                }

            }*/
            return  payService.getPayOutMessage("SUCCESS", "OK");
        }

        return  payService.getPayOutMessage("FAIL", "失败");
    }
}
